package com.FileTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {
    public static void main(String[] args) {
        storeProperties();
    }

    public static void storeProperties() {
        Properties properties = new Properties();
        properties.setProperty("name", "张三");
        properties.setProperty("age", "18");
        properties.setProperty("phone", "13684071792");
        String name = properties.getProperty("name");
        String age = properties.getProperty("age");
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("1.properties");
//            /*是将properties里的键值对 以字符的形式保存在文档里，
//            跟对象流不一样，对象流是将对象以二进制形式保存在文档里
//            * */
            properties.store(fos, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        FileInputStream fis = null;
        try {
            fis = new FileInputStream("1.properties");
            properties.load(fis);
            System.out.println(properties.getProperty("name"));
            System.out.println(properties.getProperty("phone"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
